{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/adithya-s-k/LLM-Cookbook/blob/main/Creating_News_Classification_Instruction_Dataset_using_GPT3_5.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7qnRu1H6mtkl"
      },
      "source": [
        "# Creating an Instruction Dataset for Instruction-tuning/Fine-tuning LLama 2 for News Category Prediction\n",
        "\n",
        "\n",
        "News articles play a pivotal role in machine learning research for several reasons. They contain a wealth of information, covering a wide range of topics like politics, economics, technology, and more. Moreover, they often contain complex language constructs, including metaphors, analogies, and domain-specific terminology. Utilizing this diverse and rich textual data in research and industry serves as an excellent resource for training and evaluating machine learning models, thus helping advance the field of natural language understanding and other related domains.\n",
        "\n",
        "With the diverse applications of news articles in machine learning research, from sentiment analysis to text summarization, it becomes crucial to systematically classify them into distinct categories. Not only does it help organize and structure this vast amount of data, but it also allows users to quickly access relevant news based on their research or business use case. Whether building sentiment analysis models for cryptocurrency or stock market news or conducting research in any other domain, having a well-categorized dataset is fundamental for building accurate and effective machine learning models.\n",
        "\n",
        "However, curating such a dataset manually or through keyword searches can be laborious and imprecise. In this blog, I will demonstrate how we can easily create a labeled dataset, specifically an instruction dataset, to fine-tune or instruct-tune the recently launched **Meta's Llama 2**, a powerful open-source **Large Language Model (LLM)**, for the news classification task.\n",
        "\n",
        "An instruction dataset could be created in one of the following ways:\n",
        "1. Use an existing dataset and convert it into an instruction dataset.\n",
        "2. Use existing LLMs to create an instruction dataset.\n",
        "3. Manually create an instruction dataset.\n",
        "\n",
        "Given my requirements for a high-quality dataset in a limited time and budget, I used **OpenAI's GPT 3.5**, an existing LLM that powers ChatGPT, to create an instruction dataset to instruct Llama 2 to categorize news articles into one of the 18 pre-defined categories, such as business, technology, sports, money, etc.\n",
        "\n",
        "In a follow-up notebook, I will walk through how I fine-tuned or instruct-tuned Llama 2 on my news classification instruction dataset to classify news articles into different categories.\n",
        "\n",
        "Let's get started."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9kfUqKIOSH5J"
      },
      "source": [
        "### Installing Required Libraries\n",
        "\n",
        "As a first step, I have installed the latest version of the the `openai` library to access the OpenAI API to build my news classification instruction dataset. I have also installed `datasets` from Hugging Face to view a sample instruction dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pNa_bSk9PVgM",
        "outputId": "aee7a4a8-378d-4ca3-b5fa-e3161ca2a5d7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: openai in /usr/local/lib/python3.10/dist-packages (0.27.8)\n",
            "Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai) (2.27.1)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai) (4.65.0)\n",
            "Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from openai) (3.8.5)\n",
            "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (1.26.16)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2023.7.22)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (3.4)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (23.1.0)\n",
            "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (6.0.4)\n",
            "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (4.0.2)\n",
            "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (1.9.2)\n",
            "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (1.4.0)\n",
            "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (1.3.1)\n"
          ]
        }
      ],
      "source": [
        "!pip install --upgrade openai --progress-bar off\n",
        "!pip install -Uqqq datasets --progress-bar off"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "w8UW19xfSNH-"
      },
      "source": [
        "### Loading Required Libraries\n",
        "\n",
        "Side notes on the imported modules:\n",
        "\n",
        "`tenacity` is a general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.\n",
        "\n",
        "In this notebook, I have used `tenacity` to implement exponential back-off to bypass `RateLimitError`. This error message comes from exceeding the API's rate limits.\n",
        "\n",
        "You can read more about `RateLimitError` and `tenacity` usage [over here](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_handle_rate_limits.ipynb).\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7QlAA2T5PfM5",
        "outputId": "8052f37b-7184-412f-dbf4-8e528d3be2e7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import openai\n",
        "import time\n",
        "import random\n",
        "from random import randrange\n",
        "from tenacity import retry, stop_after_attempt, wait_random_exponential, retry_if_exception_type\n",
        "from datasets import load_dataset\n",
        "import warnings\n",
        "warnings.filterwarnings('ignore')\n",
        "\n",
        "# To read and write data files in Google Drive\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive', force_remount = True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6yEsG2JSdyHD"
      },
      "source": [
        "### Sample Instruction Dataset for Text Generation\n",
        "\n",
        "Before creating an instruction dataset for the news classification task, let's look at a popular open instruction dataset, **Databricks Dolly 15K**. It contains 15,000 high-quality human-generated prompt / response pairs specifically designed for instruction tuning large language models. Read more about this dataset [over here](https://www.databricks.com/blog/2023/04/12/dolly-first-open-commercially-viable-instruction-tuned-llm)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7zxo-0qehryP"
      },
      "outputs": [],
      "source": [
        "# Sample instruction dataset\n",
        "instruction_dataset_name = \"databricks/databricks-dolly-15k\"\n",
        "\n",
        "# Loading Databricks Dolly 15K from Hugging Face Datasets\n",
        "dataset = load_dataset(instruction_dataset_name, split = \"train\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Jj2CDCKLiNGT",
        "outputId": "490d789f-ac2d-436a-d473-e88127fc12f8"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Number of prompts: 15011\n",
            "Column names are: ['instruction', 'context', 'response', 'category']\n"
          ]
        }
      ],
      "source": [
        "print(f'Number of prompts: {len(dataset)}')\n",
        "print(f'Column names are: {dataset.column_names}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "J9bSYoTU5s5C"
      },
      "source": [
        "Each prompt is a dictionary composed of 4 keys or fields.\n",
        "\n",
        "`instruction`: A question or instruction entered by the user.\n",
        "\n",
        "`context`: A text entered by the user to help interpret the instructions.\n",
        "\n",
        "`response`: Response to the instruction.\n",
        "\n",
        "`category`: Category of the instruction such as Open Q&A, Closed Q&A, Creative writing, etc."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qpthhDTtiQ3d",
        "outputId": "fcfc2df5-386b-417f-91ff-17185d5ad5f1"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{'instruction': 'What is AWS ECS?', 'context': '', 'response': 'Amazon Elastic Container Service (ECS) is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon Elastic Compute Cloud (Amazon EC2) instances.', 'category': 'open_qa'}\n"
          ]
        }
      ],
      "source": [
        "# Displaying a random prompt / response pair from the dataset\n",
        "print(dataset[randrange(len(dataset))])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9ZEjfIOt-XHF"
      },
      "source": [
        "Looking at a few more records."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cK6NGLgf789Q"
      },
      "outputs": [],
      "source": [
        "# Generating random indices\n",
        "n_samples = 10\n",
        "random_indices = random.sample(range(len(dataset)), n_samples)\n",
        "samples = []\n",
        "\n",
        "# Appending prompts to a list\n",
        "for idx in random_indices:\n",
        "    sample = dataset[idx]\n",
        "\n",
        "    sample_data = {\n",
        "        'instruction': sample['instruction'],\n",
        "        'context': sample['context'],\n",
        "        'response': sample['response'],\n",
        "        'category': sample['category']\n",
        "    }\n",
        "    samples.append(sample_data)\n",
        "\n",
        "# Creating a DataFrame\n",
        "dolly_df = pd.DataFrame(samples)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 363
        },
        "id": "vq1HQyRd9U13",
        "outputId": "00e1bb2d-860c-40ae-b829-a670013a0a22"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-b8ebe7f7-cb4c-4db4-b73d-f8d9c126ba96\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>instruction</th>\n",
              "      <th>context</th>\n",
              "      <th>response</th>\n",
              "      <th>category</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Name some of the most well-known Valyrian stee...</td>\n",
              "      <td></td>\n",
              "      <td>Widow's Wail, Heartsbane, Longclaw, Oathkeeper...</td>\n",
              "      <td>open_qa</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>What is detection engineering and what are the...</td>\n",
              "      <td></td>\n",
              "      <td>Detection engineering is a new approach to thr...</td>\n",
              "      <td>general_qa</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>How many times did Barton switch parties?</td>\n",
              "      <td>Barton switched parties three times in his pol...</td>\n",
              "      <td>Three times</td>\n",
              "      <td>information_extraction</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Tell me whether these words are English or Spa...</td>\n",
              "      <td></td>\n",
              "      <td>Dog: English\\nCat: English\\nPerro: Spanish\\nGa...</td>\n",
              "      <td>classification</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Correct the typos and grammar in this passage</td>\n",
              "      <td>Driven to investigate teh explained disappeara...</td>\n",
              "      <td>Driven to investigate the unexplained disappea...</td>\n",
              "      <td>information_extraction</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>As a child, what singer held the longest note ...</td>\n",
              "      <td></td>\n",
              "      <td>Usher</td>\n",
              "      <td>open_qa</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>What is DevSecOps?</td>\n",
              "      <td>DevSecOps is an augmentation of DevOps to allo...</td>\n",
              "      <td>DevSecOps is an augmentation of DevOps with se...</td>\n",
              "      <td>summarization</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>From this passage, extract the names of the th...</td>\n",
              "      <td>Amtrak California utilizes a livery and logo t...</td>\n",
              "      <td>Capitol Corridor, San Joaquin, and Pacific Sur...</td>\n",
              "      <td>information_extraction</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>Identify which instrument is string or percuss...</td>\n",
              "      <td></td>\n",
              "      <td>Tres is string, Tabla is percussion.</td>\n",
              "      <td>classification</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>What is the best hand in poker?</td>\n",
              "      <td></td>\n",
              "      <td>The best hand possible in poker is a Royal Flu...</td>\n",
              "      <td>open_qa</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b8ebe7f7-cb4c-4db4-b73d-f8d9c126ba96')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-81d6b60f-1beb-4f0e-9b3a-a9485b165db7\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-81d6b60f-1beb-4f0e-9b3a-a9485b165db7')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-81d6b60f-1beb-4f0e-9b3a-a9485b165db7 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-b8ebe7f7-cb4c-4db4-b73d-f8d9c126ba96 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-b8ebe7f7-cb4c-4db4-b73d-f8d9c126ba96');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "                                         instruction  \\\n",
              "0  Name some of the most well-known Valyrian stee...   \n",
              "1  What is detection engineering and what are the...   \n",
              "2          How many times did Barton switch parties?   \n",
              "3  Tell me whether these words are English or Spa...   \n",
              "4      Correct the typos and grammar in this passage   \n",
              "5  As a child, what singer held the longest note ...   \n",
              "6                                 What is DevSecOps?   \n",
              "7  From this passage, extract the names of the th...   \n",
              "8  Identify which instrument is string or percuss...   \n",
              "9                    What is the best hand in poker?   \n",
              "\n",
              "                                             context  \\\n",
              "0                                                      \n",
              "1                                                      \n",
              "2  Barton switched parties three times in his pol...   \n",
              "3                                                      \n",
              "4  Driven to investigate teh explained disappeara...   \n",
              "5                                                      \n",
              "6  DevSecOps is an augmentation of DevOps to allo...   \n",
              "7  Amtrak California utilizes a livery and logo t...   \n",
              "8                                                      \n",
              "9                                                      \n",
              "\n",
              "                                            response                category  \n",
              "0  Widow's Wail, Heartsbane, Longclaw, Oathkeeper...                 open_qa  \n",
              "1  Detection engineering is a new approach to thr...              general_qa  \n",
              "2                                        Three times  information_extraction  \n",
              "3  Dog: English\\nCat: English\\nPerro: Spanish\\nGa...          classification  \n",
              "4  Driven to investigate the unexplained disappea...  information_extraction  \n",
              "5                                              Usher                 open_qa  \n",
              "6  DevSecOps is an augmentation of DevOps with se...           summarization  \n",
              "7  Capitol Corridor, San Joaquin, and Pacific Sur...  information_extraction  \n",
              "8               Tres is string, Tabla is percussion.          classification  \n",
              "9  The best hand possible in poker is a Royal Flu...                 open_qa  "
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "display(dolly_df)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3HPaFlfpXTb2"
      },
      "source": [
        "### Defining Static Variables\n",
        "\n",
        "Let's move on to creating the instruction dataset for news classification task. In the following cell, I have defined all the static variables, including file path, file names, API key, and OpenAI model name.\n",
        "\n",
        "You can create your secret OpenAI API key at [OpenAI](https://platform.openai.com/).\n",
        "\n",
        "You can select one of the many models offered by OpenAI for prompting, such as `gpt-4`, `gpt-3.5-turbo`, `text-davinci-003`, etc. Check the complete list [over here](https://platform.openai.com/docs/models/gpt-3-5). I have used `gpt-3.5-turbo`, which powers the widely popular ChatGPT, to create my instruction dataset for news classification."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "lZmTL3taXfZD"
      },
      "outputs": [],
      "source": [
        "#### Input and output data file names ####\n",
        "path = \"/content/drive/MyDrive/\"\n",
        "input_data_filename = \"signalmedia-1m.jsonl.gz\"\n",
        "preprocessed_data_filename = \"signalmedia_news_dataset_sample.csv\"\n",
        "processed_data_filename = \"signalmedia_news_dataset_sample_classified.csv\"\n",
        "output_data_json_filename = \"news_classification.json\"\n",
        "output_data_csv_filename = \"news_classification.csv\"\n",
        "\n",
        "#### OpenAI API Key ####\n",
        "openai.api_key = \"Your OpenAI API Key\"\n",
        "\n",
        "#### OpenAI model ####\n",
        "model_name = \"gpt-3.5-turbo\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CDjF9aaWfGQl"
      },
      "source": [
        "### Preprocessing Raw Data\n",
        "\n",
        "To create a news classification dataset for instruction tuning Llama 2, I downloaded an open-source dataset named **Signal 1 Million News Articles Dataset** by **Signal AI**. This dataset, available as a zipped JSONL file, contains 1 million news articles and blogs from a variety of data sources for a period of 1 month (September 2015). There are approximately 735K news articles and 265K blog articles. I have selected only 1000 news articles for instruction tuning Llama 2 as research shows that creating a high-quality, low quantity (~1000 samples) dataset can achieve the same performance as less-quality and high quantity datasets.\n",
        "\n",
        "Data description:\n",
        "\n",
        "`id`: a unique identifier for the article\n",
        "\n",
        "`title`: the title of the article\n",
        "\n",
        "`content`: the textual content of the article (may occasionally contain HTML and JavaScript content)\n",
        "\n",
        "`source`: the name of the article source (e.g. Reuters)\n",
        "\n",
        "`published`: the publication date of the article\n",
        "\n",
        "`media-type`: either \"News\" or \"Blog\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AGIT18XBQByu"
      },
      "outputs": [],
      "source": [
        "# Reading zipped JSONL data as a Pandas DataFrame\n",
        "raw_news_df = pd.read_json(f\"{path}{input_data_filename}\", lines = True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iQMYNDJ4aPao"
      },
      "outputs": [],
      "source": [
        "# Selecting \"News\" records\n",
        "raw_news_df2 = raw_news_df[raw_news_df['media-type'] == \"News\"]\n",
        "# Shuffling the dataset\n",
        "raw_news_df3 = raw_news_df2.sample(frac = 1)\n",
        "# Selecting top 1000 records/news articles\n",
        "raw_news_df4 = raw_news_df3.head(1000)\n",
        "# Saving the preprocessed data as a CSV file\n",
        "raw_news_df4.to_csv(f\"{path}{preprocessed_data_filename}\", index = False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TGuwgbY-PvJR"
      },
      "outputs": [],
      "source": [
        "# Loading the preprocessed data as a Pandas DataFrame\n",
        "prep_news_df = pd.read_csv(f\"{path}{preprocessed_data_filename}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 658
        },
        "id": "UQne2h5DPxvb",
        "outputId": "9188d6b3-39b4-4785-e168-6d28b23fa9c5"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-690cb1d4-b9bb-4a15-bb69-b557cc4e51bd\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>content</th>\n",
              "      <th>title</th>\n",
              "      <th>media-type</th>\n",
              "      <th>source</th>\n",
              "      <th>published</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>7d6fbb3a-8ce1-46e6-ab94-b07cc6799482</td>\n",
              "      <td>SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...</td>\n",
              "      <td>Pope's trip ties Cuba to U.S., following deten...</td>\n",
              "      <td>News</td>\n",
              "      <td>Today Online</td>\n",
              "      <td>2015-09-20T08:29:19Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2da15dbf-17fc-4e95-a46a-a03dee2b9f20</td>\n",
              "      <td>Nepal will introduce a long-awaited new consti...</td>\n",
              "      <td>Nepal to introduce constitution despite deadly...</td>\n",
              "      <td>News</td>\n",
              "      <td>The Guardian Nigeria</td>\n",
              "      <td>2015-09-15T09:16:22Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>6ffd8ab6-9fc8-4c95-8f38-fab7e19aed34</td>\n",
              "      <td>This Pat Bagley cartoon appears in The Salt La...</td>\n",
              "      <td>Bagley Cartoon: Immigrant FlotsamThis Pat Bagl...</td>\n",
              "      <td>News</td>\n",
              "      <td>Hubii</td>\n",
              "      <td>2015-09-04T20:44:09Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>d49d00c8-bc27-4a5f-9af7-ff07cd34ef1d</td>\n",
              "      <td>An MP mocked the security arrangements surroun...</td>\n",
              "      <td>Ups and downs at the Labour Conference from We...</td>\n",
              "      <td>News</td>\n",
              "      <td>Mirror.co.uk</td>\n",
              "      <td>2015-09-29T21:38:10Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>bd2293db-d4ff-49a0-8e13-fd7d62aecc32</td>\n",
              "      <td>IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...</td>\n",
              "      <td>Uniden Launches New Small Business Communicati...</td>\n",
              "      <td>News</td>\n",
              "      <td>Good Day Sacramento</td>\n",
              "      <td>2015-09-29T12:55:00Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>995</th>\n",
              "      <td>9d6a936c-6339-4fad-8cd4-e7d87e6c51fb</td>\n",
              "      <td>With increased focus on career and technology ...</td>\n",
              "      <td>CISD increases, creates stipends for CTE programs</td>\n",
              "      <td>News</td>\n",
              "      <td>The Villager</td>\n",
              "      <td>2015-09-15T04:02:14Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>996</th>\n",
              "      <td>6d91732b-f4f1-4e0a-bd71-77d096c6943c</td>\n",
              "      <td>About 345\\r\\nBrown County Water Utility custom...</td>\n",
              "      <td>300+ water customers under boil advisory</td>\n",
              "      <td>News</td>\n",
              "      <td>Greetings From Brown County</td>\n",
              "      <td>2015-09-09T19:31:16Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>997</th>\n",
              "      <td>669abbb0-e9eb-4b46-8fda-612858494c58</td>\n",
              "      <td>\\n\\r Nikica Jelavic has expressed his deligh...</td>\n",
              "      <td>Official: West Ham Confirm Nikica Jelavic Sign...</td>\n",
              "      <td>News</td>\n",
              "      <td>Inside Futbol</td>\n",
              "      <td>2015-09-01T11:30:49Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>998</th>\n",
              "      <td>2bef7386-ef2f-4658-9243-20b96e84dc44</td>\n",
              "      <td>A pretty quiet day in the cash dairy markets o...</td>\n",
              "      <td>Cash cheese and butter steady on Tuesday</td>\n",
              "      <td>News</td>\n",
              "      <td>Brownfield Network</td>\n",
              "      <td>2015-09-08T21:28:37Z</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>999</th>\n",
              "      <td>3ab37bd9-60dd-4f85-8d47-5af528acefde</td>\n",
              "      <td>After Monday’s trading in UnitedHealth Group I...</td>\n",
              "      <td>UnitedHealth Group (UNH) Showing Neutral Techn...</td>\n",
              "      <td>News</td>\n",
              "      <td>Market Intelligence Center</td>\n",
              "      <td>2015-09-15T12:00:00Z</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>1000 rows × 6 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-690cb1d4-b9bb-4a15-bb69-b557cc4e51bd')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-fab08135-e3d6-4e32-bdb0-2d9b3a16e00d\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-fab08135-e3d6-4e32-bdb0-2d9b3a16e00d')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-fab08135-e3d6-4e32-bdb0-2d9b3a16e00d button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-690cb1d4-b9bb-4a15-bb69-b557cc4e51bd button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-690cb1d4-b9bb-4a15-bb69-b557cc4e51bd');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "                                       id  \\\n",
              "0    7d6fbb3a-8ce1-46e6-ab94-b07cc6799482   \n",
              "1    2da15dbf-17fc-4e95-a46a-a03dee2b9f20   \n",
              "2    6ffd8ab6-9fc8-4c95-8f38-fab7e19aed34   \n",
              "3    d49d00c8-bc27-4a5f-9af7-ff07cd34ef1d   \n",
              "4    bd2293db-d4ff-49a0-8e13-fd7d62aecc32   \n",
              "..                                    ...   \n",
              "995  9d6a936c-6339-4fad-8cd4-e7d87e6c51fb   \n",
              "996  6d91732b-f4f1-4e0a-bd71-77d096c6943c   \n",
              "997  669abbb0-e9eb-4b46-8fda-612858494c58   \n",
              "998  2bef7386-ef2f-4658-9243-20b96e84dc44   \n",
              "999  3ab37bd9-60dd-4f85-8d47-5af528acefde   \n",
              "\n",
              "                                               content  \\\n",
              "0    SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...   \n",
              "1    Nepal will introduce a long-awaited new consti...   \n",
              "2    This Pat Bagley cartoon appears in The Salt La...   \n",
              "3    An MP mocked the security arrangements surroun...   \n",
              "4    IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...   \n",
              "..                                                 ...   \n",
              "995  With increased focus on career and technology ...   \n",
              "996  About 345\\r\\nBrown County Water Utility custom...   \n",
              "997    \\n\\r Nikica Jelavic has expressed his deligh...   \n",
              "998  A pretty quiet day in the cash dairy markets o...   \n",
              "999  After Monday’s trading in UnitedHealth Group I...   \n",
              "\n",
              "                                                 title media-type  \\\n",
              "0    Pope's trip ties Cuba to U.S., following deten...       News   \n",
              "1    Nepal to introduce constitution despite deadly...       News   \n",
              "2    Bagley Cartoon: Immigrant FlotsamThis Pat Bagl...       News   \n",
              "3    Ups and downs at the Labour Conference from We...       News   \n",
              "4    Uniden Launches New Small Business Communicati...       News   \n",
              "..                                                 ...        ...   \n",
              "995  CISD increases, creates stipends for CTE programs       News   \n",
              "996           300+ water customers under boil advisory       News   \n",
              "997  Official: West Ham Confirm Nikica Jelavic Sign...       News   \n",
              "998           Cash cheese and butter steady on Tuesday       News   \n",
              "999  UnitedHealth Group (UNH) Showing Neutral Techn...       News   \n",
              "\n",
              "                          source             published  \n",
              "0                   Today Online  2015-09-20T08:29:19Z  \n",
              "1           The Guardian Nigeria  2015-09-15T09:16:22Z  \n",
              "2                          Hubii  2015-09-04T20:44:09Z  \n",
              "3                   Mirror.co.uk  2015-09-29T21:38:10Z  \n",
              "4            Good Day Sacramento  2015-09-29T12:55:00Z  \n",
              "..                           ...                   ...  \n",
              "995                 The Villager  2015-09-15T04:02:14Z  \n",
              "996  Greetings From Brown County  2015-09-09T19:31:16Z  \n",
              "997                Inside Futbol  2015-09-01T11:30:49Z  \n",
              "998           Brownfield Network  2015-09-08T21:28:37Z  \n",
              "999   Market Intelligence Center  2015-09-15T12:00:00Z  \n",
              "\n",
              "[1000 rows x 6 columns]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "display(prep_news_df)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wFsxfOfvDXrd"
      },
      "source": [
        "Although we can combine `title` and `content` together, I have only used the `content` column in subsequent cells to create the instruction dataset."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "no4yNyvWEQYE"
      },
      "source": [
        "### Creating Custom Prompt Template\n",
        "\n",
        "In the following cell, I have created a custom prompt template to interact with GPT 3.5. It would define bot behavior and instruct it to categorize news articles provided by the user into one of the 43 categories. I found these categories from the News Category Dataset on [Kaggle](https://www.kaggle.com/datasets/rmisra/news-category-dataset). This dataset contains 210K news headlines and their categories extracted from HuffPost between 2012 to 2021.\n",
        "\n",
        "I have also used **Few Shot Prompting** to guide the model to respond in a specific way by providing two news articles and their expected output as examples."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1-vf_8ytQbLO"
      },
      "outputs": [],
      "source": [
        "# Defining bot behavior and instructing\n",
        "SYSTEM_PROMPT = \"\"\"You are ChatGPT, an intelligent bot. I will give you a news article. You have to classify the news into one of the 43 categories.\"\"\"\n",
        "\n",
        "USER_PROMPT_1 = \"\"\"Are you clear about your role?\"\"\"\n",
        "\n",
        "ASSISTANT_PROMPT_1 = \"\"\"Sure, I'm ready to help you with your news classification task. Please provide me with the necessary information to get started.\"\"\"\n",
        "\n",
        "# Few Shot Prompting\n",
        "PROMPT = (\n",
        "\"\"\"\n",
        "Categories:\n",
        "\n",
        "U.S. NEWS\n",
        "COMEDY\n",
        "PARENTING\n",
        "WORLD NEWS\n",
        "CULTURE & ARTS\n",
        "TECH\n",
        "SPORTS\n",
        "ENTERTAINMENT\n",
        "POLITICS\n",
        "WEIRD NEWS\n",
        "ENVIRONMENT\n",
        "EDUCATION\n",
        "CRIME\n",
        "SCIENCE\n",
        "WELLNESS\n",
        "BUSINESS\n",
        "STYLE & BEAUTY\n",
        "FOOD & DRINK\n",
        "MEDIA\n",
        "QUEER VOICES\n",
        "HOME & LIVING\n",
        "WOMEN\n",
        "BLACK VOICES\n",
        "TRAVEL\n",
        "MONEY\n",
        "RELIGION\n",
        "LATINO VOICES\n",
        "IMPACT\n",
        "WEDDINGS\n",
        "COLLEGE\n",
        "PARENTS\n",
        "ARTS & CULTURE\n",
        "STYLE\n",
        "GREEN\n",
        "TASTE\n",
        "HEALTHY LIVING\n",
        "THE WORLDPOST\n",
        "GOOD NEWS\n",
        "WORLDPOST\n",
        "FIFTY\n",
        "ARTS\n",
        "DIVORCE\n",
        "ESG\n",
        "\n",
        "If you don't know the category, response \"OTHERS\".\n",
        "\n",
        "Output Format:\n",
        "Category name\n",
        "\n",
        "Examples:\n",
        "1. News: New Product Gives Marketers Access to Real Keywords, Conversions and Results Along With 13 Months of Historical Data\n",
        "\n",
        "SAN FRANCISCO, CA -- (Marketwired) -- 09/17/15 -- Jumpshot, a marketing analytics company that uses distinctive data sources to paint a complete picture of the online customer journey, today announced the launch of Jumpshot Elite, giving marketers insight into what their customers are doing the 99% of the time they're not on your site. For years, marketers have been unable to see what organic and paid search terms users were entering, much less tie those searches to purchases. Jumpshot not only injects that user search visibility back into the market, but also makes it possible to tie those keywords to conversions -- for any web site.\n",
        "\n",
        "\"Ever since search engines encrypted search results, marketers have been in the dark about keywords, impacting not only the insight into their own search investments, but also their ability to unearth high converting keywords for their competitors,\" said Deren Baker, CEO of Jumpshot. \"Our platform eliminates the hacks, assumptions, and guesswork that marketers are doing now and provides real data: actual searches tied to actual conversions conducted by real people with nothing inferred.\"\n",
        "\n",
        "Unlike other keyword research tools that receive data through the Adwords API or send bots to cobble together various data inputs and implied metrics, Jumpshot leverages its panel of over 115 million global consumers to analyze real search activity. As a result, Jumpshot is able to provide companies with actionable data to improve the ROI of their search marketing campaigns, SEO tactics and content marketing initiatives.\n",
        "\n",
        "Available today, Jumpshot Elite provides 13 months of backward-looking data as well as:\n",
        "\n",
        "Access to real queries used by searchers\n",
        "\n",
        "Paid and organic results for any website\n",
        "\n",
        "Visibility into organic keywords, eliminating the \"not provided\" outcome in web analytics\n",
        "\n",
        "Real user queries, clicks and transactions instead of machine-generated clicks with inferred results\n",
        "\n",
        "Ability to tie keywords to real transactions on any website\n",
        "\n",
        "Variable attribution models and lookback windows\n",
        "\n",
        "Launched in January, 2015, Jumpshot grew out of the ambitions of a group of smart marketers and data scientists who were frustrated about the limitations of the data they had access to, and excited about the opportunity to provide new insights into online behavior.\n",
        "\n",
        "The company uses distinctive data sources to paint a complete picture of the online world for businesses, from where customers spend time online to what they do there and how they get from place to place. By tracking the online customer journey down to each click, Jumpshot reveals how and why customers arrive at purchase decisions. The company tracks more data in more detail than other services, tracking 160 billion monthly clicks generated by its extensive data panel.\n",
        "\n",
        "About Jumpshot\n",
        "\n",
        "Jumpshot is a marketing analytics platform that reveals the entire customer journey -- from the key sources of traffic to a site, to browsing and buying behavior on any domain. With a panel of 115 million users, Jumpshot provides marketers with the insight to understand what their customers are doing the 99% of the time they're not on their own site -- a scope of information never before attainable. Jumpshot was founded in 2015 and is headquartered in San Francisco.\n",
        "\n",
        "For more information, please visit www.jumpshot.com.\n",
        "\n",
        "Image Available: http://www2.marketwire.com/mw/frame_mw?attachid=2889222\n",
        "\n",
        "Kelly Mayes\n",
        "\n",
        "The Bulleit Group\n",
        "\n",
        "615-200-8845\n",
        "\n",
        "Published Sep. 17, 2015\n",
        "\n",
        "Copyright © 2015 SYS-CON Media, Inc. — All Rights Reserved.\n",
        "\n",
        "Syndicated stories and blog feeds, all rights reserved by the author.\n",
        "\n",
        "Output: TECHNOLOGY\n",
        "\n",
        "2. News: SOURCE Harwood Feffer LLP\n",
        "\n",
        "NEW YORK\n",
        "\n",
        "On July 21, 2015\n",
        "\n",
        "On this news, VASCO stock nearly 33% and has not recovered.\n",
        "\n",
        "Our investigation concerns whether the Company board of directors has breached its fiduciary duties to shareholders, grossly mismanaged the Company, and/or committed abuses of control in connection with the foregoing.\n",
        "\n",
        "If you own VASCO shares and wish to discuss this matter with us, or have any questions concerning your rights and interests with regard to this matter, please contact:\n",
        "\n",
        "Robert I. Harwood, Esq.\n",
        "\n",
        "Harwood Feffer\n",
        "\n",
        "The law firm responsible for this advertisement is Harwood Feffer LLP (www.hfesq.com). Prior results do not guarantee or predict a similar outcome with respect to any future matter.\n",
        "\n",
        "Logo - http://photos.prnewswire.com/prnh/20120215/MM54604LOGO\n",
        "\n",
        "To view the original version on PR Newswire, visit:http://www.prnewswire.com/news-releases/harwood-feffer-llp-announces-investigation-of-vasco-data-security-international-inc-300149371.html\n",
        "\n",
        "©2015 PR Newswire. All Rights Reserved.\n",
        "\n",
        "Output: BUSINESS\n",
        "\n",
        "3. {}\n",
        "Output:\n",
        "\"\"\"\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QbKujkaQIR0c"
      },
      "source": [
        "### Generating Model Inference\n",
        "\n",
        "In the following cells, I have defined `chat_completion_with_backoff` and `openai_chat_completion_response` functions to send user prompts and receive response using OpenAI's Chat Completion API.\n",
        "\n",
        "`tenacity.retry` decorator implements automatic retry requests with a random exponential backoff to avoid rate limit errors. Retrying with exponential backoff means performing a short sleep when a rate limit error is hit, then retrying the unsuccessful request. If the request is still unsuccessful, the sleep length is increased and the process is repeated. This continues until the request is successful or until a maximum number of retries is reached.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2HmEGbXCQdQy"
      },
      "outputs": [],
      "source": [
        "# Decorator for automatic retry requests\n",
        "@retry(\n",
        "    retry = retry_if_exception_type((openai.error.APIError, openai.error.APIConnectionError, openai.error.RateLimitError, openai.error.ServiceUnavailableError, openai.error.Timeout)),\n",
        "    # Function to add random exponential backoff to a request\n",
        "    wait = wait_random_exponential(multiplier = 1, max = 60),\n",
        "    stop = stop_after_attempt(10)\n",
        ")\n",
        "\n",
        "# Function to invoke Open AI's Chat Complete AI\n",
        "def chat_completion_with_backoff(**kwargs):\n",
        "    return openai.ChatCompletion.create(**kwargs)\n",
        "\n",
        "# Function to pass model name and user prompts and receive response\n",
        "def openai_chat_completion_response(USER_PROMPT_2):\n",
        "  response = chat_completion_with_backoff(\n",
        "              model = model_name,\n",
        "              messages = [\n",
        "                    {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
        "                    {\"role\": \"user\", \"content\": USER_PROMPT_1},\n",
        "                    {\"role\": \"assistant\", \"content\": ASSISTANT_PROMPT_1},\n",
        "                    {\"role\": \"user\", \"content\": USER_PROMPT_2}\n",
        "                ]\n",
        "            )\n",
        "\n",
        "  return response['choices'][0]['message']['content'].strip(\" \\n\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zJ937LQrMwQ5"
      },
      "source": [
        "Next, I have defined `predict_news_category` function that accepts a news article from the preprocessed dataset, appends it to the user prompt, and sends the prompt to `openai_chat_completion_response` function for classification. The output would be one of the 43 pre-defined news categories if the request went through successfully, otherwise it would be NA. One of the other reasons apart from rate limit that might interrupt the API call is exceeding token limit. In such cases, we could trim news articles with high token count to get a valid response.\n",
        "\n",
        "`predict_news_category` is called through a lambda function that iterates over every row of the `content` column in the preprocessed dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-e7kyHIoQi3H"
      },
      "outputs": [],
      "source": [
        "# Function to classify news articles\n",
        "def predict_news_category(news_body):\n",
        "  # Add news article to the prompt\n",
        "  NEWS = news_body\n",
        "  FINAL_PROMPT = PROMPT.format(NEWS)\n",
        "  # Send prompt for inference\n",
        "  try:\n",
        "    classify_news = openai_chat_completion_response(FINAL_PROMPT)\n",
        "  except:\n",
        "    # Output \"NA\" if the request fails\n",
        "    classify_news = \"NA\"\n",
        "  time.sleep(20)\n",
        "  return classify_news"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "OXEak_gbQlHa"
      },
      "outputs": [],
      "source": [
        "# Selecting 100 records at a time for inference\n",
        "prep_news_df2 = prep_news_df.iloc[0:100,:].copy()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vuLjk5NiQwIW"
      },
      "outputs": [],
      "source": [
        "# Lambda function to iterate over news articles and save response as a new column\n",
        "prep_news_df2['predicted_category'] = prep_news_df2['content'].apply(lambda x: predict_news_category(x))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "LKEzx8sAINb2",
        "outputId": "c706a7ef-4cbc-4962-decd-2615ac73d9e7"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-d499fcde-bac0-430f-a04a-843d87ff986d\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>content</th>\n",
              "      <th>predicted_category</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...</td>\n",
              "      <td>WORLD NEWS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Nepal will introduce a long-awaited new consti...</td>\n",
              "      <td>WORLD NEWS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>This Pat Bagley cartoon appears in The Salt La...</td>\n",
              "      <td>CULTURE &amp; ARTS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>An MP mocked the security arrangements surroun...</td>\n",
              "      <td>POLITICS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...</td>\n",
              "      <td>TECH</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d499fcde-bac0-430f-a04a-843d87ff986d')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-71908f05-4d52-44e6-b4b0-ffa09d770db3\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-71908f05-4d52-44e6-b4b0-ffa09d770db3')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-71908f05-4d52-44e6-b4b0-ffa09d770db3 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-d499fcde-bac0-430f-a04a-843d87ff986d button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-d499fcde-bac0-430f-a04a-843d87ff986d');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "                                             content predicted_category\n",
              "0  SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...         WORLD NEWS\n",
              "1  Nepal will introduce a long-awaited new consti...         WORLD NEWS\n",
              "2  This Pat Bagley cartoon appears in The Salt La...     CULTURE & ARTS\n",
              "3  An MP mocked the security arrangements surroun...           POLITICS\n",
              "4  IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...               TECH"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "display(prep_news_df2[['content', 'predicted_category']].head())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vVXaD4xOQy1Q"
      },
      "outputs": [],
      "source": [
        "# Saving output file\n",
        "prep_news_df2.to_csv(f\"{path}{processed_data_filename}\", index = False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QRHcL_8sQxxj"
      },
      "source": [
        "Looking at the results, GPT 3.5 could accurately classify most of the news into one of the 43 categories. The predicted categories look perfect! Due to usage limit, I could only infer 100 news articles at the time I prepared this notebook. While other batches are still processing and might take some time to finish, I went ahead with converting these 100 records into an instruction dataset for fine-tuning Llama 2."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W-4mGDlhX0or"
      },
      "source": [
        "### Creating Instruction Dataset\n",
        "\n",
        "In the following cells, I have analyzed the model results further and created an instruction dataset that follows the same structure as that of **Databricks Dolly 15K**."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dDFrN9J-Q3Kk"
      },
      "outputs": [],
      "source": [
        "# Loading processed data as a Pandas DataFrame\n",
        "prep_news_df2 = pd.read_csv(f\"{path}{processed_data_filename}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 658
        },
        "id": "aUukJS-uXxW4",
        "outputId": "d1548538-5072-4dfd-e271-2ae631ba83c3"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-b25b6960-5d86-420d-ba9c-2fd9a8c703cf\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>content</th>\n",
              "      <th>title</th>\n",
              "      <th>media-type</th>\n",
              "      <th>source</th>\n",
              "      <th>published</th>\n",
              "      <th>predicted_category</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>7d6fbb3a-8ce1-46e6-ab94-b07cc6799482</td>\n",
              "      <td>SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...</td>\n",
              "      <td>Pope's trip ties Cuba to U.S., following deten...</td>\n",
              "      <td>News</td>\n",
              "      <td>Today Online</td>\n",
              "      <td>2015-09-20T08:29:19Z</td>\n",
              "      <td>WORLD NEWS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2da15dbf-17fc-4e95-a46a-a03dee2b9f20</td>\n",
              "      <td>Nepal will introduce a long-awaited new consti...</td>\n",
              "      <td>Nepal to introduce constitution despite deadly...</td>\n",
              "      <td>News</td>\n",
              "      <td>The Guardian Nigeria</td>\n",
              "      <td>2015-09-15T09:16:22Z</td>\n",
              "      <td>WORLD NEWS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>6ffd8ab6-9fc8-4c95-8f38-fab7e19aed34</td>\n",
              "      <td>This Pat Bagley cartoon appears in The Salt La...</td>\n",
              "      <td>Bagley Cartoon: Immigrant FlotsamThis Pat Bagl...</td>\n",
              "      <td>News</td>\n",
              "      <td>Hubii</td>\n",
              "      <td>2015-09-04T20:44:09Z</td>\n",
              "      <td>COMEDY</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>d49d00c8-bc27-4a5f-9af7-ff07cd34ef1d</td>\n",
              "      <td>An MP mocked the security arrangements surroun...</td>\n",
              "      <td>Ups and downs at the Labour Conference from We...</td>\n",
              "      <td>News</td>\n",
              "      <td>Mirror.co.uk</td>\n",
              "      <td>2015-09-29T21:38:10Z</td>\n",
              "      <td>POLITICS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>bd2293db-d4ff-49a0-8e13-fd7d62aecc32</td>\n",
              "      <td>IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...</td>\n",
              "      <td>Uniden Launches New Small Business Communicati...</td>\n",
              "      <td>News</td>\n",
              "      <td>Good Day Sacramento</td>\n",
              "      <td>2015-09-29T12:55:00Z</td>\n",
              "      <td>TECH</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>95</th>\n",
              "      <td>bf968a79-2356-4451-868a-c6dcd82ddf5b</td>\n",
              "      <td>A man donning a full-faced safety helmet stood...</td>\n",
              "      <td>Penang jewellery shop heist caught on video</td>\n",
              "      <td>News</td>\n",
              "      <td>New Straits Times</td>\n",
              "      <td>2015-09-29T06:46:17Z</td>\n",
              "      <td>CRIME</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>96</th>\n",
              "      <td>eea0db32-3780-491d-83eb-9c6846d019e3</td>\n",
              "      <td>Pharma 411   http://t.co/7WfIBSsXz5 #biotech  ...</td>\n",
              "      <td>Jaguar Animal Health Signs Crofelemer Manufact...</td>\n",
              "      <td>News</td>\n",
              "      <td>NewsR.in</td>\n",
              "      <td>2015-09-28T13:25:57Z</td>\n",
              "      <td>TECH</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>97</th>\n",
              "      <td>e3e52b3c-e561-4f40-8e17-7c554825ce90</td>\n",
              "      <td>SOURCE Express Scripts\\n\\nST. LOUIS \\n\\nMr. Sl...</td>\n",
              "      <td>Express Scripts Names Eric Slusser Chief Finan...</td>\n",
              "      <td>News</td>\n",
              "      <td>14 WFIE</td>\n",
              "      <td>2015-09-10T21:23:00Z</td>\n",
              "      <td>TECHNOLOGY</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>98</th>\n",
              "      <td>972c064c-7505-4b73-beda-710f8a285430</td>\n",
              "      <td>Date: September 6-8, 2015 \\n\\nLocation: Olympi...</td>\n",
              "      <td>International Jewellrey London 2015</td>\n",
              "      <td>News</td>\n",
              "      <td>Euromonitor International</td>\n",
              "      <td>2015-09-14T23:53:44Z</td>\n",
              "      <td>BUSINESS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>99</th>\n",
              "      <td>2dd9c5c3-fdda-47d9-9450-595aee298899</td>\n",
              "      <td>Tweet [embedded content] \\r Tweet [embedded co...</td>\n",
              "      <td>Watch Hailee Steinfeld Get Empowered In Her De...</td>\n",
              "      <td>News</td>\n",
              "      <td>Free-i-News</td>\n",
              "      <td>2015-09-18T16:13:27Z</td>\n",
              "      <td>ENTERTAINMENT</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>100 rows × 7 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b25b6960-5d86-420d-ba9c-2fd9a8c703cf')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-970e0d46-4ae6-4fb6-9f77-090f222fe411\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-970e0d46-4ae6-4fb6-9f77-090f222fe411')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-970e0d46-4ae6-4fb6-9f77-090f222fe411 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-b25b6960-5d86-420d-ba9c-2fd9a8c703cf button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-b25b6960-5d86-420d-ba9c-2fd9a8c703cf');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "                                      id  \\\n",
              "0   7d6fbb3a-8ce1-46e6-ab94-b07cc6799482   \n",
              "1   2da15dbf-17fc-4e95-a46a-a03dee2b9f20   \n",
              "2   6ffd8ab6-9fc8-4c95-8f38-fab7e19aed34   \n",
              "3   d49d00c8-bc27-4a5f-9af7-ff07cd34ef1d   \n",
              "4   bd2293db-d4ff-49a0-8e13-fd7d62aecc32   \n",
              "..                                   ...   \n",
              "95  bf968a79-2356-4451-868a-c6dcd82ddf5b   \n",
              "96  eea0db32-3780-491d-83eb-9c6846d019e3   \n",
              "97  e3e52b3c-e561-4f40-8e17-7c554825ce90   \n",
              "98  972c064c-7505-4b73-beda-710f8a285430   \n",
              "99  2dd9c5c3-fdda-47d9-9450-595aee298899   \n",
              "\n",
              "                                              content  \\\n",
              "0   SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...   \n",
              "1   Nepal will introduce a long-awaited new consti...   \n",
              "2   This Pat Bagley cartoon appears in The Salt La...   \n",
              "3   An MP mocked the security arrangements surroun...   \n",
              "4   IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...   \n",
              "..                                                ...   \n",
              "95  A man donning a full-faced safety helmet stood...   \n",
              "96  Pharma 411   http://t.co/7WfIBSsXz5 #biotech  ...   \n",
              "97  SOURCE Express Scripts\\n\\nST. LOUIS \\n\\nMr. Sl...   \n",
              "98  Date: September 6-8, 2015 \\n\\nLocation: Olympi...   \n",
              "99  Tweet [embedded content] \\r Tweet [embedded co...   \n",
              "\n",
              "                                                title media-type  \\\n",
              "0   Pope's trip ties Cuba to U.S., following deten...       News   \n",
              "1   Nepal to introduce constitution despite deadly...       News   \n",
              "2   Bagley Cartoon: Immigrant FlotsamThis Pat Bagl...       News   \n",
              "3   Ups and downs at the Labour Conference from We...       News   \n",
              "4   Uniden Launches New Small Business Communicati...       News   \n",
              "..                                                ...        ...   \n",
              "95        Penang jewellery shop heist caught on video       News   \n",
              "96  Jaguar Animal Health Signs Crofelemer Manufact...       News   \n",
              "97  Express Scripts Names Eric Slusser Chief Finan...       News   \n",
              "98                International Jewellrey London 2015       News   \n",
              "99  Watch Hailee Steinfeld Get Empowered In Her De...       News   \n",
              "\n",
              "                       source             published predicted_category  \n",
              "0                Today Online  2015-09-20T08:29:19Z         WORLD NEWS  \n",
              "1        The Guardian Nigeria  2015-09-15T09:16:22Z         WORLD NEWS  \n",
              "2                       Hubii  2015-09-04T20:44:09Z             COMEDY  \n",
              "3                Mirror.co.uk  2015-09-29T21:38:10Z           POLITICS  \n",
              "4         Good Day Sacramento  2015-09-29T12:55:00Z               TECH  \n",
              "..                        ...                   ...                ...  \n",
              "95          New Straits Times  2015-09-29T06:46:17Z              CRIME  \n",
              "96                   NewsR.in  2015-09-28T13:25:57Z               TECH  \n",
              "97                    14 WFIE  2015-09-10T21:23:00Z         TECHNOLOGY  \n",
              "98  Euromonitor International  2015-09-14T23:53:44Z           BUSINESS  \n",
              "99                Free-i-News  2015-09-18T16:13:27Z      ENTERTAINMENT  \n",
              "\n",
              "[100 rows x 7 columns]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "display(prep_news_df2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 802
        },
        "id": "BOfq9en1YIJU",
        "outputId": "ed58a3ed-a43c-4cec-8b5d-8ecd2144c6cc"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-41b00774-a52e-467d-bc67-bda1ec31ed5c\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>predicted_category</th>\n",
              "      <th>count</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>BUSINESS</td>\n",
              "      <td>15</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>POLITICS</td>\n",
              "      <td>12</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>SPORTS</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>ENTERTAINMENT</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>OTHERS</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>TECH</td>\n",
              "      <td>6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>WORLD NEWS</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>HEALTHY LIVING</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>EDUCATION</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>TECHNOLOGY</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>10</th>\n",
              "      <td>CRIME</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>11</th>\n",
              "      <td>ENVIRONMENT</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>12</th>\n",
              "      <td>MEDIA</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>13</th>\n",
              "      <td>FOOD &amp; DRINK</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>14</th>\n",
              "      <td>SCIENCE</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>15</th>\n",
              "      <td>CULTURE &amp; ARTS</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>16</th>\n",
              "      <td>SPACE</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>17</th>\n",
              "      <td>ARTS &amp; CULTURE</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>18</th>\n",
              "      <td>RELIGION</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>19</th>\n",
              "      <td>MARKETING &amp; ADVERTISING</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>20</th>\n",
              "      <td>MONEY</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>21</th>\n",
              "      <td>COMEDY</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>22</th>\n",
              "      <td>FINANCE</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>23</th>\n",
              "      <td>NaN</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-41b00774-a52e-467d-bc67-bda1ec31ed5c')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-691b2e5d-c871-451e-8825-dda0e0daa839\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-691b2e5d-c871-451e-8825-dda0e0daa839')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-691b2e5d-c871-451e-8825-dda0e0daa839 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-41b00774-a52e-467d-bc67-bda1ec31ed5c button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-41b00774-a52e-467d-bc67-bda1ec31ed5c');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "         predicted_category  count\n",
              "0                  BUSINESS     15\n",
              "1                  POLITICS     12\n",
              "2                    SPORTS      9\n",
              "3             ENTERTAINMENT      9\n",
              "4                    OTHERS      8\n",
              "5                      TECH      6\n",
              "6                WORLD NEWS      5\n",
              "7            HEALTHY LIVING      4\n",
              "8                 EDUCATION      4\n",
              "9                TECHNOLOGY      4\n",
              "10                    CRIME      4\n",
              "11              ENVIRONMENT      3\n",
              "12                    MEDIA      3\n",
              "13             FOOD & DRINK      2\n",
              "14                  SCIENCE      2\n",
              "15           CULTURE & ARTS      2\n",
              "16                    SPACE      1\n",
              "17           ARTS & CULTURE      1\n",
              "18                 RELIGION      1\n",
              "19  MARKETING & ADVERTISING      1\n",
              "20                    MONEY      1\n",
              "21                   COMEDY      1\n",
              "22                  FINANCE      1\n",
              "23                      NaN      1"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Frequency distribution of predicted news categories\n",
        "pred_cat_freq_dist = prep_news_df2['predicted_category'].value_counts(dropna = False).sort_values(ascending = False).reset_index()\n",
        "pred_cat_freq_dist = pred_cat_freq_dist.rename(columns = {\"index\": \"predicted_category\", \"predicted_category\": \"count\"})\n",
        "display(pred_cat_freq_dist)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LG6wBphmaITJ"
      },
      "source": [
        "Due to the small sample size, only 23 categories could be captured. `BUSINESS`, `POLITICS`, `SPORTS`, and `ENTERTAINMENT` are the top 4 categories.\n",
        "\n",
        "The model also generated new categories, such as `TECHNOLOGY`, `SPACE`, `MARKETING & ADVERTISING`, and `FINANCE`. To further preprocess the dataset, I have combined these categories with relevant existing categories. For example: `TECHNOLOGY` is merged with `TECH`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "W-dE5g35cBFC"
      },
      "outputs": [],
      "source": [
        "# Merging new news categories with existing ones\n",
        "prep_news_df2['predicted_category'] = np.where(prep_news_df2['predicted_category'] == \"TECHNOLOGY\", \"TECH\", prep_news_df2['predicted_category'])\n",
        "prep_news_df2['predicted_category'] = np.where(prep_news_df2['predicted_category'] == \"SPACE\", \"SCIENCE\", prep_news_df2['predicted_category'])\n",
        "prep_news_df2['predicted_category'] = np.where(prep_news_df2['predicted_category'] == \"FINANCE\", \"MONEY\", prep_news_df2['predicted_category'])\n",
        "prep_news_df2['predicted_category'] = np.where(prep_news_df2['predicted_category'] == \"MARKETING & ADVERTISING\", \"OTHERS\", prep_news_df2['predicted_category'])\n",
        "prep_news_df2['predicted_category'] = np.where(prep_news_df2['predicted_category'] == \"ARTS & CULTURE\", \"CULTURE & ARTS\", prep_news_df2['predicted_category'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 645
        },
        "id": "n1Ro6iawdlbK",
        "outputId": "adde0322-a503-4ebf-8dd7-f09533a17ac4"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-8ab6fa34-ce6a-41f6-b70d-e6863c11d695\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>predicted_category</th>\n",
              "      <th>count</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>BUSINESS</td>\n",
              "      <td>15</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>POLITICS</td>\n",
              "      <td>12</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>TECH</td>\n",
              "      <td>10</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>SPORTS</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>OTHERS</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>ENTERTAINMENT</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>WORLD NEWS</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>CRIME</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>EDUCATION</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>HEALTHY LIVING</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>10</th>\n",
              "      <td>ENVIRONMENT</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>11</th>\n",
              "      <td>SCIENCE</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>12</th>\n",
              "      <td>CULTURE &amp; ARTS</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>13</th>\n",
              "      <td>MEDIA</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>14</th>\n",
              "      <td>FOOD &amp; DRINK</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>15</th>\n",
              "      <td>MONEY</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>16</th>\n",
              "      <td>RELIGION</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>17</th>\n",
              "      <td>COMEDY</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>18</th>\n",
              "      <td>NaN</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-8ab6fa34-ce6a-41f6-b70d-e6863c11d695')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-76a25723-27c6-4f46-a79b-2978cf7c6c55\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-76a25723-27c6-4f46-a79b-2978cf7c6c55')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-76a25723-27c6-4f46-a79b-2978cf7c6c55 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-8ab6fa34-ce6a-41f6-b70d-e6863c11d695 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-8ab6fa34-ce6a-41f6-b70d-e6863c11d695');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "   predicted_category  count\n",
              "0            BUSINESS     15\n",
              "1            POLITICS     12\n",
              "2                TECH     10\n",
              "3              SPORTS      9\n",
              "4              OTHERS      9\n",
              "5       ENTERTAINMENT      9\n",
              "6          WORLD NEWS      5\n",
              "7               CRIME      4\n",
              "8           EDUCATION      4\n",
              "9      HEALTHY LIVING      4\n",
              "10        ENVIRONMENT      3\n",
              "11            SCIENCE      3\n",
              "12     CULTURE & ARTS      3\n",
              "13              MEDIA      3\n",
              "14       FOOD & DRINK      2\n",
              "15              MONEY      2\n",
              "16           RELIGION      1\n",
              "17             COMEDY      1\n",
              "18                NaN      1"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Frequency distribution of updated predicted news categories\n",
        "pred_cat_freq_dist = prep_news_df2['predicted_category'].value_counts(dropna = False).sort_values(ascending = False).reset_index()\n",
        "pred_cat_freq_dist = pred_cat_freq_dist.rename(columns = {\"index\": \"predicted_category\", \"predicted_category\": \"count\"})\n",
        "display(pred_cat_freq_dist)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hm2XFCpzd0jF"
      },
      "source": [
        "Excluding NA, there are 18 news categories in the dataset that could be used to fine-tune LLMs."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tCLopgGlfF86"
      },
      "source": [
        "In the final step, I have created a constant column named `instruction`, akin to the `instruction` column in the Databricks Dolly 15K dataset, that contains the instruction to classify the news article into one of the 18 categories. Then, I have filtered out \"NA\" news category record and renamed `content` to `input` (the equivalent of `context` in Databricks Dolly 15K) and `predicted_category` to `output` (the equivalent of `response` in Databricks Dolly 15K) before saving the `DataFrame` as a JSON file and a CSV file."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZgX7jrO2RF2Z"
      },
      "outputs": [],
      "source": [
        "# Creating instruction against each news article / news category pairs\n",
        "prep_news_df2['instruction'] = \"\"\"Categorize the news article into one of the 18 categories:\n",
        "\n",
        "WORLD NEWS\n",
        "COMEDY\n",
        "POLITICS\n",
        "TECH\n",
        "SPORTS\n",
        "BUSINESS\n",
        "OTHERS\n",
        "ENTERTAINMENT\n",
        "CULTURE & ARTS\n",
        "FOOD & DRINK\n",
        "MEDIA\n",
        "RELIGION\n",
        "MONEY\n",
        "HEALTHY LIVING\n",
        "SCIENCE\n",
        "EDUCATION\n",
        "CRIME\n",
        "ENVIRONMENT\n",
        "\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2rNmqEJuRKzI"
      },
      "outputs": [],
      "source": [
        "# Removing null news category records\n",
        "prep_news_df3 = prep_news_df2[~prep_news_df2['predicted_category'].isna()]\n",
        "\n",
        "# Renaming and selecting relevant columns\n",
        "prep_news_df4 = prep_news_df3.rename(columns = {'content': 'input', 'predicted_category': 'output'})\n",
        "output_news_df = prep_news_df4[['instruction', 'input', 'output']]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 423
        },
        "id": "tAjCvTNqh9qn",
        "outputId": "f37296ae-ff20-48b5-efb5-d69672617418"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-6a7642ef-312a-4a08-aece-2609649895e3\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>instruction</th>\n",
              "      <th>input</th>\n",
              "      <th>output</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...</td>\n",
              "      <td>WORLD NEWS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>Nepal will introduce a long-awaited new consti...</td>\n",
              "      <td>WORLD NEWS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>This Pat Bagley cartoon appears in The Salt La...</td>\n",
              "      <td>COMEDY</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>An MP mocked the security arrangements surroun...</td>\n",
              "      <td>POLITICS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...</td>\n",
              "      <td>TECH</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>95</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>A man donning a full-faced safety helmet stood...</td>\n",
              "      <td>CRIME</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>96</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>Pharma 411   http://t.co/7WfIBSsXz5 #biotech  ...</td>\n",
              "      <td>TECH</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>97</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>SOURCE Express Scripts\\n\\nST. LOUIS \\n\\nMr. Sl...</td>\n",
              "      <td>TECH</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>98</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>Date: September 6-8, 2015 \\n\\nLocation: Olympi...</td>\n",
              "      <td>BUSINESS</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>99</th>\n",
              "      <td>Categorize the news article into one of the 18...</td>\n",
              "      <td>Tweet [embedded content] \\r Tweet [embedded co...</td>\n",
              "      <td>ENTERTAINMENT</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>99 rows × 3 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-6a7642ef-312a-4a08-aece-2609649895e3')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-c494b264-e010-488c-a6a6-bbede99f8ef8\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-c494b264-e010-488c-a6a6-bbede99f8ef8')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-c494b264-e010-488c-a6a6-bbede99f8ef8 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-6a7642ef-312a-4a08-aece-2609649895e3 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-6a7642ef-312a-4a08-aece-2609649895e3');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "                                          instruction  \\\n",
              "0   Categorize the news article into one of the 18...   \n",
              "1   Categorize the news article into one of the 18...   \n",
              "2   Categorize the news article into one of the 18...   \n",
              "3   Categorize the news article into one of the 18...   \n",
              "4   Categorize the news article into one of the 18...   \n",
              "..                                                ...   \n",
              "95  Categorize the news article into one of the 18...   \n",
              "96  Categorize the news article into one of the 18...   \n",
              "97  Categorize the news article into one of the 18...   \n",
              "98  Categorize the news article into one of the 18...   \n",
              "99  Categorize the news article into one of the 18...   \n",
              "\n",
              "                                                input         output  \n",
              "0   SANTIAGO DE CUBA, Cuba - Pope Francis wraps up...     WORLD NEWS  \n",
              "1   Nepal will introduce a long-awaited new consti...     WORLD NEWS  \n",
              "2   This Pat Bagley cartoon appears in The Salt La...         COMEDY  \n",
              "3   An MP mocked the security arrangements surroun...       POLITICS  \n",
              "4   IRVING, Texas , Sept. 29, 2015 /PRNewswire/ --...           TECH  \n",
              "..                                                ...            ...  \n",
              "95  A man donning a full-faced safety helmet stood...          CRIME  \n",
              "96  Pharma 411   http://t.co/7WfIBSsXz5 #biotech  ...           TECH  \n",
              "97  SOURCE Express Scripts\\n\\nST. LOUIS \\n\\nMr. Sl...           TECH  \n",
              "98  Date: September 6-8, 2015 \\n\\nLocation: Olympi...       BUSINESS  \n",
              "99  Tweet [embedded content] \\r Tweet [embedded co...  ENTERTAINMENT  \n",
              "\n",
              "[99 rows x 3 columns]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "display(output_news_df)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QVM0lRInRMzZ"
      },
      "outputs": [],
      "source": [
        "# Converting to list of dictionaries\n",
        "news_json = output_news_df.to_json(orient = 'records', lines = True).splitlines()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Yz1AHkmYiCEM",
        "outputId": "2623b7bf-318b-4904-abe3-807cc2f74db1"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\"instruction\":\"Categorize the news article into one of the 18 categories:\\n\\nWORLD NEWS\\nCOMEDY\\nPOLITICS\\nTECH\\nSPORTS\\nBUSINESS\\nOTHERS\\nENTERTAINMENT\\nCULTURE & ARTS\\nFOOD & DRINK\\nMEDIA\\nRELIGION\\nMONEY\\nHEALTHY LIVING\\nSCIENCE\\nEDUCATION\\nCRIME\\nENVIRONMENT\\n\\n\",\"input\":\"SANTIAGO DE CUBA, Cuba - Pope Francis wraps up his visit to Cuba on Tuesday and heads to the United States, figuratively connecting the two longtime Cold War adversaries who have reached detente with the help of his mediation. \\n\\nThe 78-year-old Argentine pope will celebrate Mass at the sanctuary of the Virgin of Charity of El Cobre, the country's holiest shrine and one also venerated by non-believers and practitioners of Afro-Cuban religions infused with varying degrees of Catholicism. \\n  \\nAt El Cobre on Monday, Francis prayed for reconciliation among all Cubans, both at home and around the world. \\n\\nAn estimated 2 million Cubans have left the island since the 1959 revolution with some 1.3 million currently living abroad, most of them in the United States, where many exiles remain bitterly estranged from their homeland. \\n\\nThere is great anticipation for what Francis will say in the United States, where he will meet with U.S. President Barack Obama, deliver the first address by a pope before Congress, and speak at the United Nations. \\n\\nThe pope avoided making overt political statements in Cuba, as dissidents had hoped he would, but used his homilies to send messages laced in spirituality about the need for change in the one-party Communist country. \\n\\nHe urged Cubans to think out of the box and be tolerant of other people's ideas. At a Mass on Monday for tens of thousands of people in the eastern city of Holguin, he urged his listeners \\\"not to be satisfied with appearances or with what is politically correct.\\\" \\n\\nThe gentler approach, a contrast to the tack taken by his two immediate predecessors when they visited, seems driven by a desire to quietly encourage Cubans at a delicate time following the resumption of diplomatic ties with the United States. Meanwhile the Cuban Church is discreetly negotiating greater space for its mission. \\n\\n\\\"He has spoken with clarity, discretion and restraint,\\\" Vatican spokesman Federico Lombardi told reporters, when asked why the pope had not spoken out directly about issues such as Cuba's human rights record and the U.S. trade embargo, which the Vatican opposes. \\n\\n\\\"The pope wants to make a contribution but the responsibility lies with the leaders of nations. He does not want to exaggerate his role, he just wants to contribute by making suggestions, promoting dialogue, justice and the common good of people,\\\" he said. REUTERS\",\"output\":\"WORLD NEWS\"}\n"
          ]
        }
      ],
      "source": [
        "print(news_json[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SaPK6sH5RQky"
      },
      "outputs": [],
      "source": [
        "# Saving as a JSON file\n",
        "with open(f\"{path}{output_data_json_filename}\", 'w') as f:\n",
        "    for line in news_json:\n",
        "        f.write(f\"{line}\\n\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RJtNQluzRR7w"
      },
      "outputs": [],
      "source": [
        "# Saving as a CSV file\n",
        "output_news_df.to_csv(f\"{path}{output_data_csv_filename}\", index = False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Wy7Rwt4AlJdU"
      },
      "source": [
        "### Conclusion\n",
        "\n",
        "In this notebook, I have leveraged GPT 3.5, a powerful Large Language Model, to create a labelled dataset for news categorization. This dataset consists of approximately 100 high-quality records (work in progress to add more sample) and was produced with minimal human intervention.\n",
        "\n",
        "In an upcoming Google Colab notebook, I will demonstrate how to build a custom news classifier by fine-tuning / instruct-tuning Llama 2 on this dataset and categorize news articles into one of the several categories."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "include_colab_link": true,
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
